package com.example.luyao.faceserver;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserFeatureDB {
    private SQLiteDBHelper helper;
    private SQLiteDatabase db;

    public UserFeatureDB(Context context) {
        helper = new SQLiteDBHelper(context);
        db = helper.getWritableDatabase();
    }

    public Long addUserFeature(String name, String feature, String user_id, String remark, byte[] image) {
        ContentValues cv = new ContentValues();
        cv.put("picture", image);
        Long picture_id = -1L;
        db.beginTransaction();
        try {
            picture_id = db.insert(SQLiteDBHelper.TABLE_PICTURE, null, cv);
            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }

        if(-1 == picture_id) return picture_id;
        cv.clear();
        cv.put("name", name);
        cv.put("feature", feature);
        cv.put("picture_id", picture_id);
        cv.put("user_id", user_id);
        cv.put("remark", remark);
        db.beginTransaction();
        try {
            Long id = db.insert(SQLiteDBHelper.TABLE_FEATURE, null, cv);
            db.setTransactionSuccessful();
            return id;
        } finally {
            db.endTransaction();
        }
    }

    public Long updateUserFeature(int feature_id, String name, String feature, String user_id, String remark,
                                  byte[] image, int picture_id_old) {
        ContentValues cv = new ContentValues();
        Long picture_id = -1L;
        if(image != null) {
            cv.put("picture", image);
            db.beginTransaction();
            try {
                picture_id = db.insert(SQLiteDBHelper.TABLE_PICTURE, null, cv);
                db.setTransactionSuccessful();
            } finally {
                db.endTransaction();
            }
        }

        if(image != null && -1 == picture_id) return picture_id;
        cv.clear();
        cv.put("name", name);
        if(image != null) {
            cv.put("feature", feature);
            cv.put("picture_id", picture_id);
        }
        cv.put("user_id", user_id);
        cv.put("remark", remark);
        db.beginTransaction();
        try {
            if(image != null) {
                db.delete(SQLiteDBHelper.TABLE_PICTURE,
                        "id = ?", new String[]{String.valueOf(picture_id_old)});
            }
            db.update(SQLiteDBHelper.TABLE_FEATURE, cv, "feature_id = ?",
                      new String[]{String.valueOf(feature_id)});
            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
        return Long.valueOf(feature_id);
    }

    public boolean deleteUserFeatureById(int feature_id, int picture_id_old) {
        boolean ret = false;
        db.beginTransaction();
        try {
            db.delete(SQLiteDBHelper.TABLE_PICTURE, "id = ?", new String[]{String.valueOf(picture_id_old)});
            db.delete(SQLiteDBHelper.TABLE_FEATURE, "feature_id = ?",
                      new String[]{String.valueOf(feature_id)});
            db.setTransactionSuccessful();
            ret = true;
        } finally {
            db.endTransaction();
        }
        return ret;
    }

    public void deleteAllUserFeature() {
        db.beginTransaction();
        try {
            db.delete(SQLiteDBHelper.TABLE_PICTURE, null, new String[]{});
            db.delete(SQLiteDBHelper.TABLE_FEATURE, null, new String[]{});
            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    }

    public byte[] queryUserPicture(int picture_id) {
        Cursor cursor = db.rawQuery(
                "select * from " + SQLiteDBHelper.TABLE_PICTURE + " where id = ? limit 1",
                new String[]{String.valueOf(picture_id)});
        while (cursor.moveToNext()) {
            byte[] ret = cursor.getBlob(cursor.getColumnIndex("picture"));
            return ret;
        }
        return null;
    }

    public List<Map<String, Object>> queryAllUserFeature() {
        ArrayList<Map<String, Object>> user_feature = new ArrayList<>();
        Cursor cursor = db.rawQuery(
            "select * from " + SQLiteDBHelper.TABLE_FEATURE + " order by feature_id asc", new String[] {});
        while (cursor.moveToNext()) {
            HashMap<String, Object> content = new HashMap<>();
            content.put("feature_id", cursor.getInt(cursor.getColumnIndex("feature_id")));
            content.put("name", cursor.getString(cursor.getColumnIndex("name")));

            String feature_str = cursor.getString(cursor.getColumnIndex("feature"));
            String[] feature_str_list = feature_str.split(",");
            int feature_length = 512;
            float[] feature_float = new float[feature_length];
            for(int i = 0; i < feature_length; i++) {
                feature_float[i] = Float.parseFloat(feature_str_list[i]);
            }
            content.put("feature", feature_float);
            content.put("picture_id", cursor.getInt(cursor.getColumnIndex("picture_id")));
            content.put("user_id", cursor.getString(cursor.getColumnIndex("user_id")));
            content.put("remark", cursor.getString(cursor.getColumnIndex("remark")));
            user_feature.add(content);
        }
        cursor.close();
        return user_feature;
    }

    public void close() {
        db.close();
    }
}
